﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OneShoppingDAL;
using OneShoppingBLL;
using OneShoppingUtility;
using OneShoppingModel;

public partial class Backstage_Pages_sysmng_userInfoAdd : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (role.RoleId != 1)
        {
            Response.Redirect("login.html");
        }
        if(!IsPostBack)
        {
            BindDDL();
        }
    }
    //绑定数据源
    private void BindDDL()
    {
        UtilityDataBind.DropDownListBindData<Role>(ref rolelist,
            RoleBLL.GetInstance().GetList(), "RoleName", "RoleId", "角色");
    }
    //返回按钮
    protected void btnReturn_Click(object sender, EventArgs e)
    {
        Response.Redirect("userManager.aspx");
    }
    //保存按钮
    protected void btnSave_Click(object sender, EventArgs e)
    {
        msg_role.Visible = false;
        msg_psw.Visible = false;
        msg_phone.Visible = false;
        msg_name.Visible = false;
        if(rolelist.SelectedValue=="-1")
        {
            msg_role.InnerText = "请选择角色";
            msg_role.Visible = true;
            return;
        }
        if (loginName.Text.Length == 0)
        {
            msg_name.InnerText = "请输入用户名";
            msg_name.Visible = true;
            return;
        }
        if (password.Text.Length < 5)
        {
            msg_psw.InnerText = "密码不能小于6位";
            msg_psw.Visible = true;
            return;
        }
        if(phone.Text.Length!=11)
        {
            msg_phone.InnerText = "请输入正确的手机号";
            msg_phone.Visible = true;
            return;
        }
        var username = SystemUserBLL.GetInstance().GetEntity(r => r.LoginName == loginName.Text);
        if(username!=null)
        {
            msg_name.InnerText = "用户名重复";
            msg_name.Visible = true;
            return;
        }
        SystemUser user = new SystemUser 
        {
            RoleId=Convert.ToInt32(rolelist.SelectedValue),
            LoginName=loginName.Text,
            LoginPwd=password.Text,
            UserPhone=phone.Text,
            Status=1,
        };
        var reslut = SystemUserBLL.GetInstance().Insert(user);
        if(reslut)
        {
            var u = SystemUserBLL.GetInstance().GetEntity(r => r.LoginName == loginName.Text);
            switch(rolelist.SelectedValue)
            {
                case "1":
                    CompanyUser c = new CompanyUser
                    {
                        UserId = u.UserId
                    };
                    bool rc=CompanyUserBLL.GetInstance().Insert(c);
                    if(!rc)
                    {
                        SystemUserBLL.GetInstance().Delete(u.UserId);
                        UtilityMessageBox.Show(this, "添加失败");
                        return;
                    }
                    break;
                case "2":
                    BusinessUser b = new BusinessUser
                    {
                        UserId = u.UserId
                    };
                    bool rb = BusinessUserBLL.GetInstance().Insert(b);
                    if(!rb)
                    {
                        SystemUserBLL.GetInstance().Delete(u.UserId);
                        UtilityMessageBox.Show(this, "添加失败");
                        return;
                    }
                    break;
                case "3":
                    GeneralUser g = new GeneralUser
                    {
                        UserId = u.UserId
                    };
                    bool rg = GeneralUserBLL.GetInstance().Insert(g);
                    if(!rg)
                    {
                        SystemUserBLL.GetInstance().Delete(u.UserId);
                        UtilityMessageBox.Show(this, "添加失败");
                        return;
                    }
                    break;
            }
            UtilityMessageBox.ShowAndRedirect(this, "添加成功", "userManager.aspx");
        }
        else
        {
            UtilityMessageBox.Show(this, "添加异常");
        }
    }
}